package com.ssy.lingxi.after.sale.controller.mobile;

import com.ssy.lingxi.after.sale.api.model.vo.request.CommonIdVO;
import com.ssy.lingxi.after.sale.model.constant.ReturnGoodsReasonEnum;
import com.ssy.lingxi.after.sale.model.vo.request.*;
import com.ssy.lingxi.after.sale.model.vo.response.*;
import com.ssy.lingxi.after.sale.service.*;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @menu App - 售后退货相关接口
 * @author 伍将
 * @date 2020/12/30
 * @version 2.0.0
 */
@RestController
@RequestMapping("/as/mobile/returnGoods")
public class MobileReturnGoodsController extends BaseController {

    @Resource
    private IReturnGoodsService returnService;

    @Resource
    private IReturnGoodsDetailService returnedGoodsService;

    @Resource
    private IInnerWorkflowRecordService innerWorkflowRecordService;

    @Resource
    private IOuterWorkflowRecordService outerWorkflowRecordService;

    @Resource
    private IReturnDeliveryGoodsService returnDeliveryGoodsService;

    /**
     * 分页查询采购商售后退货
     * @author 伍将
     * @date 2020/12/30
     **/
/*    @RequestMapping(value = "/pageByConsumer", method = RequestMethod.GET)
    public Wrapper<PageData<ReturnGoodsQueryVO>> pageByConsumer(HttpServletRequest httpRequest, @Valid ReturnGoodsPageVO pageVO) {
        return returnService.pageByConsumer(getSysUser(httpRequest), pageVO);
    }*/

    /**
     * 退货原因-列表
     * @return
     */
    @RequestMapping(value = "/getReasonList", method = RequestMethod.GET)
    public Wrapper<List<DropdownItemVO>> getReasonList() {
        return Wrapper.success(ReturnGoodsReasonEnum.toDropdownItems());
    }

    /**
     * 分页查询-采购商售后退货-新
     * @param httpRequest
     * @param pageVO
     * @return
     */
    @RequestMapping(value = "/pageByConsumer", method = RequestMethod.GET)
    public Wrapper<PageData<ReturnGoodsQueryAppVO>> pageByConsumer(HttpServletRequest httpRequest, @Valid ReturnGoodsPageAppVO pageVO) {
        return returnService.pageByConsumerApp(getSysUser(httpRequest), pageVO);
    }

    /**
     * 查询采购商售后退货详情
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/getDetailByConsumer", method = RequestMethod.GET)
    public Wrapper<ReturnGoodsDetailAppVO> getDetailByConsumer(HttpServletRequest httpRequest, Long replaceId) {
        return returnService.getDetailByApp(getSysUser(httpRequest), replaceId);
    }

    /**
     * 分页查询采购商售后退货商品
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/pageGoodsByConsumer", method = RequestMethod.GET)
    public Wrapper<PageData<ReturnGoodsDetailAppQueryVO>> pageGoodsByConsumer(@Valid ReturnGoodsDetailPageVO pageVO) {
        return returnedGoodsService.pageListByApp(pageVO);
    }

    /**
     * 分页查询采购商售后退货内部流转记录
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/pageInnerWorkflowRecordByConsumer", method = RequestMethod.GET)
    public Wrapper<PageData<ReturnInnerWorkflowRecordVO>> pageInnerWorkflowRecordByConsumer(HttpServletRequest httpRequest, @Valid WorkflowPageVO pageVO) {
        return innerWorkflowRecordService.pageReturnRecord(getSysUser(httpRequest), pageVO);
    }

    /**
     * 分页查询采购商售后退货外部流转记录
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/pageOuterWorkflowRecordByConsumer", method = RequestMethod.GET)
    public Wrapper<PageData<ReturnOuterWorkflowRecordVO>> pageOuterWorkflowRecordByConsumer(HttpServletRequest httpRequest, @Valid WorkflowPageVO pageVO) {
        return outerWorkflowRecordService.pageReturnRecord(getSysUser(httpRequest), pageVO);
    }

    /**
     * 分页查询采购商待提交退货申请单
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/pageToBeSubmitByConsumer", method = RequestMethod.GET)
    public Wrapper<PageData<ReturnGoodsQueryVO>> pageToBeSubmitByConsumer(HttpServletRequest httpRequest, @Valid ReturnGoodsPageVO pageVO) {
        return returnService.pageToBeSubmitByConsumer(getSysUser(httpRequest), pageVO);
    }

    /**
     * 采购商保存退货申请单
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/saveByConsumer", method = RequestMethod.POST)
    public Wrapper<Long> saveByConsumer(HttpServletRequest httpRequest, @RequestBody @Valid ReturnGoodsSaveVO saveVO) {
        Wrapper<Long> save = returnService.save(getSysUser(httpRequest), saveVO);
        if(save.getCode()== ResponseCode.SUCCESS.getCode()) {
            Wrapper<Void> submit = returnService.submit(getSysUser(httpRequest), save.getData());
            if(submit.getCode()!= ResponseCode.SUCCESS.getCode()){
                return Wrapper.fail(submit.getCode(),submit.getMessage());
            }
        }
        return save;
    }

    /**
     * 采购商删除退货申请单
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/deleteByConsumer", method = RequestMethod.POST)
    public Wrapper<Void> deleteByConsumer(HttpServletRequest httpRequest, @RequestBody @Valid CommonIdVO commonIdVO) {
        return returnService.delete(getSysUser(httpRequest), commonIdVO.getDataId());
    }

    /**
     * 采购商提交退货申请单
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/submitByConsumer", method = RequestMethod.POST)
    public Wrapper<Void> submitByConsumer(HttpServletRequest httpRequest, @RequestBody @Valid CommonIdVO commonIdVO) {
        return returnService.submit(getSysUser(httpRequest), commonIdVO.getDataId());
    }

    /**
     * 分页查询采购商待新增退货发货单
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/pageToBeAddReturnDeliveryGoods", method = RequestMethod.GET)
    public Wrapper<PageData<ReturnGoodsQueryVO>> pageToBeAddReturnDeliveryGoods(HttpServletRequest httpRequest, @Valid ReturnGoodsPageVO pageVO) {
        return returnService.pageToBeAddReturnDeliveryGoods(getSysUser(httpRequest), pageVO);
    }

    /**
     * 待新增退货发货单-新增退货发货单
     * @author 伍将
     * @date 2020/9/24
     **/
    @RequestMapping(value ="/addReturnDeliveryGoods", method = RequestMethod.POST)
    public Wrapper<Long> addReturnDeliveryGoods(HttpServletRequest httpRequest,@RequestBody @Valid ReturnDeliveryGoodsAddVO addVO) {
        return returnService.addReturnDeliveryGoods(getSysUser(httpRequest),addVO);
    }

    /**
     *  待新增退货发货单-详情
     * @date 2021/8/13
     * @version 2.0.0
     */
    @RequestMapping(value = "/getReturnDeliveryGoodsDetail", method = RequestMethod.GET)
    public Wrapper<ReturnGoodsBatchDetailVO> getReturnDeliveryGoodsDetail(HttpServletRequest httpRequest,Long returnDeliveryId) {
        return returnService.getReturnGoodsDeliveryOrReceiveDetail(getSysUser(httpRequest), returnDeliveryId,false);
    }

    /**
     * 待新增退货发货单-审核退货发货单
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/verifyReturnDeliveryGoods", method = RequestMethod.POST)
    public Wrapper<Void> verifyReturnDeliveryGoods(HttpServletRequest httpRequest, @RequestBody CommonIdVO commonIdVO) {
        return returnService.verifyReturnDeliveryGoodsByConsumer(getSysUser(httpRequest), commonIdVO.getDataId());
    }

    /**
     * 分页查询采购商待新增物流单
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/pageToBeAddLogisticsByConsumer", method = RequestMethod.GET)
    public Wrapper<PageData<ReturnGoodsQueryVO>> pageToBeAddLogisticsByConsumer(HttpServletRequest httpRequest, @Valid ReturnGoodsPageVO pageVO) {
        return returnService.pageToBeAddLogisticsByConsumer(getSysUser(httpRequest), pageVO);
    }

    /**
     * 分页查询采购商待退货发货单
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/pageToBeReturnDeliveryGoods", method = RequestMethod.GET)
    public Wrapper<PageData<ReturnGoodsQueryVO>> pageToBeReturnDeliveryGoods(HttpServletRequest httpRequest, @Valid ReturnGoodsPageVO pageVO) {
        return returnService.pageToBeReturnDeliveryGoods(getSysUser(httpRequest), pageVO);
    }

    /**
     * 采购商确认退货发货
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/confirmReturnDeliveryGoods", method = RequestMethod.POST)
    public Wrapper<Void> confirmReturnDeliveryGoods(HttpServletRequest httpRequest, @RequestBody @Valid CommonIdVO commonIdVO) {
        return returnService.confirmReturnDeliveryGoods(getSysUser(httpRequest), commonIdVO.getDataId());
    }

    /**
     * 采购商手工退货发货[819]
     * @param httpRequest
     * @param deliveryGoodsVO
     * @return
     */
    @RequestMapping(value = "/manualReturnDeliveryGoods", method = RequestMethod.POST)
    public Wrapper<Void> manualReturnDeliveryGoods(HttpServletRequest httpRequest, @RequestBody @Valid ReturnManualGoodsVO deliveryGoodsVO) {
        return returnService.manualReturnDeliveryGoodsCommon(getSysUser(httpRequest), deliveryGoodsVO);
    }

    /**
     * 分页查询采购商待确认退货回单
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/pageToBeConfirmReturnGoodsReceipt", method = RequestMethod.GET)
    public Wrapper<PageData<ReturnGoodsQueryVO>> pageToBeConfirmReturnGoodsReceipt(HttpServletRequest httpRequest, @Valid ReturnGoodsPageVO pageVO) {
        return returnService.pageToBeConfirmReturnGoodsReceipt(getSysUser(httpRequest), pageVO);
    }

    /**
     * 采购商确认退货回单
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/confirmReturnGoodsReceipt", method = RequestMethod.POST)
    public Wrapper<Void> confirmReturnGoodsReceipt(HttpServletRequest httpRequest, @RequestBody @Valid CommonIdVO commonIdVO) {
        return returnService.confirmReturnGoodsReceipt(getSysUser(httpRequest), commonIdVO.getDataId());
    }

    /**
     * 采购商继续退货发货[废弃：通过确认回单就行状态流转]
     * @author 伍将
     * @date 2020/12/30
     **/
    /*@RequestMapping(value = "/continueReturnDeliveryGoods", method = RequestMethod.POST)*/
    public Wrapper<Void> continueReturnDeliveryGoods(HttpServletRequest httpRequest, @RequestBody @Valid CommonIdVO commonIdVO) {
        return returnService.continueReturnDeliveryGoods(getSysUser(httpRequest), commonIdVO.getDataId());
    }

    /**
     * 采购商确认全部退货回单[废弃：通过确认回单就行状态流转]
     * @author 伍将
     * @date 2020/12/30
     **/
    /*@RequestMapping(value = "/confirmAllReturnGoodsReceipt", method = RequestMethod.POST)*/
    public Wrapper<Void> confirmAllReturnGoodsReceipt(HttpServletRequest httpRequest, @RequestBody @Valid CommonIdVO commonIdVO) {
        return returnService.confirmAllReturnGoodsReceipt(getSysUser(httpRequest), commonIdVO.getDataId());
    }

    /**
     * 分页查询采购商待确认退款申请单
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/pageToBeConfirmRefund", method = RequestMethod.GET)
    public Wrapper<PageData<ReturnGoodsQueryVO>> pageToBeConfirmRefund(HttpServletRequest httpRequest, @Valid ReturnGoodsPageVO pageVO) {
        return returnService.pageToBeConfirmRefund(getSysUser(httpRequest), pageVO);
    }

    /**
     * 采购商确认退款
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/confirmRefund", method = RequestMethod.POST)
    public Wrapper<Void> confirmRefund(HttpServletRequest httpRequest, @RequestBody @Valid ReturnGoodsConfirmRefundVO confirmRefundVO) {
        return returnService.confirmRefund(getSysUser(httpRequest), confirmRefundVO);
    }

    /**
     * 采购商确认全部退款
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/confirmAllRefund", method = RequestMethod.POST)
    public Wrapper<Void> confirmAllRefund(HttpServletRequest httpRequest, @RequestBody @Valid CommonIdVO commonIdVO) {
        return returnService.confirmAllRefund(getSysUser(httpRequest), commonIdVO.getDataId());
    }

    /**
     * 分页查询采购商待确认售后完成申请单
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/pageToBeComplete", method = RequestMethod.GET)
    public Wrapper<PageData<ReturnGoodsQueryVO>> pageToBeComplete(HttpServletRequest httpRequest, @Valid ReturnGoodsPageVO pageVO) {
        return returnService.pageToBeComplete(getSysUser(httpRequest), pageVO);
    }

    /**
     * 采购商确认售后完成
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/confirmComplete", method = RequestMethod.POST)
    public Wrapper<Void> confirmComplete(HttpServletRequest httpRequest, @RequestBody @Valid ReturnConfirmCompleteVO completeVO) {
        return returnService.confirmComplete(getSysUser(httpRequest), completeVO);
    }

    /**
     * 分页查询列表页面搜索条件内容
     * @author 伍将
     * @date 2020/10/17
     **/
    @RequestMapping(value = "/pageItems", method = RequestMethod.GET)
    public Wrapper<ReturnGoodsPageItemsVO> pageItems(HttpServletRequest httpRequest) {
        return returnService.pageItems(getSysUser(httpRequest));
    }

    /**
     * 采购商分页查询退款商品
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/pageRefundByConsumer", method = RequestMethod.GET)
    public Wrapper<PageData<ReturnGoodsRefundVO>> pageRefundByConsumer(@Valid ReturnGoodsDetailPageVO pageVO) {
        return returnedGoodsService.pageRefundByConsumer(pageVO);
    }

    /**
     * 待新增物流单-分页获取退货商品
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/pageCommodityByLogistics", method = RequestMethod.GET)
    public Wrapper<PageData<CommodityQueryVO>> pageCommodityByLogistics(@Valid CommodityPageVO pageVO) {
        return returnDeliveryGoodsService.pageCommodityByLogistics(pageVO);
    }

    /**
     * 物流服务-获取退货申请单详情
     * @author 伍将
     * @date 2020/12/9
     **/
    @RequestMapping(value = "/pageDetailByLogistics", method = RequestMethod.GET)
    public Wrapper<ApplyOrderQueryVO> pageDetailByLogistics(Long returnId) {
        return returnService.pageDetailByLogistics(returnId);
    }
}
